package com.rest;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.service.EncripService;
import com.vo.RestResponse;

@RestController
@RequestMapping(value="url")
public class UrlRest {

	@Autowired
	EncripService encripService;

	@RequestMapping(value="encript")
	public RestResponse urlEncipt(@RequestParam("url")String url,
			@RequestParam(value="key",defaultValue="")String key){
		RestResponse response=new RestResponse();
		if(!url.startsWith("http")){
			url="http://"+url;
		}
		try{
			String result=encripService.encrip(url, key);
			response.setData(result);
		}catch(Exception e){
			response.setCode(500);
		}
		return response;
	}

	@RequestMapping("sendDirct")
	public RestResponse sendDirct(@RequestParam("url")String url){
		RestResponse response=new RestResponse();
		try {
			String redirct=encripService.findUrl(url, "url");
			if(redirct==null||redirct.equals(""))
				response.setCode(300);
			else
				response.setData(redirct);
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(500);
		}
		return response;
	}

}
